package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OrderDetailsContract;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.presenter.OrderDetailsPresenter;
import com.shd.edanpei.ui.view.CarDataView;
import com.shd.edanpei.ui.view.CustomerDataView;
import com.shd.edanpei.ui.view.CustomerNeedView;
import com.shd.edanpei.ui.view.EnterpriseDataView;
import com.shd.edanpei.ui.view.HouseDataView;
import com.shd.edanpei.ui.view.OrderDataView;
import com.shd.edanpei.ui.view.PersonalDataView;
import com.shd.edanpei.util.DensityUtil;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/20.
 * 抢单详情
 */

public class TakeOrderDetailsActivity extends BaseActivity<OrderDetailsPresenter> implements OrderDetailsContract.OrderDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.tv_take)
    TextView tvTake;

    private int orderID;
    private int index;
    private OrderDetailsModel model;

    @Override
    protected int setLayout() {
        return R.layout.activity_takeorderdetails;
    }

    @Override
    protected OrderDetailsPresenter bindPresenter() {
        return new OrderDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("订单详情");
        Bundle extras = getIntent().getExtras();
        orderID = extras.getInt(Constant.ORDER_ID);
        index = extras.getInt(Constant.INDEX);
        mPresenter.requetData(orderID);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    /**
     * 抢单
     */
    @OnClick(R.id.tv_take)
    void take() {
        if (model.getBaseInfo().getUserStatus() == 2) {
            mPresenter.take(orderID, index);
        } else {
            //已经被接单
            ToastUtil.showToast("已被接单");
        }
    }

    @Subscribe(code = RxBusCode.TAKE_ORDER_SUCCESS)
    public void takeOrderSuccess(StatusBean bean) {
        tvTake.setText("抢单成功");
        tvTake.setBackground(getResources().getDrawable(R.drawable.shape_btn_unclick));
        tvTake.setClickable(false);
    }

    @Subscribe(code = RxBusCode.TAKE_ORDER_FAIL)
    public void takeFail(StatusBean bean) {
        tvTake.setText("抢单失败");
        tvTake.setBackground(getResources().getDrawable(R.drawable.shape_btn_unclick));
        tvTake.setClickable(false);
    }

    @Override
    protected void onDestroy() {
        container.removeAllViews();
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

    @Override
    public void setData(OrderDetailsModel model) {
        this.model = model;
        model.setPrivacy(true);
        container.addView(new OrderDataView(this, model));
        container.addView(new CustomerNeedView(this, model));
        container.addView(new CustomerDataView(this, model));
        if (model.getBaseInfo().getOrderType() == OrderTypeEnum.car.getKey()) {
            container.addView(new CarDataView(this, model));
        } else if (model.getBaseInfo().getOrderType() == OrderTypeEnum.house.getKey()) {
            container.addView(new HouseDataView(this, model));
        }else if(model.getBaseInfo().getOrderType()==OrderTypeEnum.personal.getKey()){
            container.addView(new PersonalDataView(this, model));
        }else if(model.getBaseInfo().getOrderType()==OrderTypeEnum.enterprise.getKey()){
            container.addView(new EnterpriseDataView(this, model));
        }
        //此view是为了与底部有一段距离
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, DensityUtil.dp2px(this, 15));
        View view = new View(this);
        view.setLayoutParams(params);
        container.addView(view);

        if (model.getBaseInfo().getUserStatus() == 2) {
            //未被接单
            tvTake.setBackground(ContextCompat.getDrawable(this, R.drawable.shape_main_button));
            tvTake.setText("我要接单");
        } else {
            //已经被接单
            tvTake.setBackground(ContextCompat.getDrawable(this, R.drawable.shape_rad6dp_gray));
            tvTake.setText("已被接单");
        }
        tvTake.setVisibility(View.VISIBLE);
    }
}
